from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
import threading
from proc_info import get_procinfo, get_pid

def async_call(fn):
    def wrapper(*args, **kwargs):
        threading.Thread(target=fn, args=args, kwargs=kwargs).start()
    return wrapper

class Ui_Process(QDialog):
    def __init__(self, proc_name):
        super().__init__()
        self.setFixedSize(800, 600)
        self.setWindowTitle(proc_name + "进程监控")
        
        self.textBrowser_process = QtWidgets.QTextBrowser()
        self.layout_ui()
        self.update_timer = QTimer() 
        self.update_timer.start(1000)  
        self.update_timer.timeout.connect(self.update_view)
        self.procinfo = ""
        self.proc_name = proc_name
        self.pid = get_pid(proc_name)
    
    def layout_ui(self):
        
        self.v_box = QVBoxLayout()
        self.v_box.addWidget(self.textBrowser_process)
        self.setLayout(self.v_box)
        
    @async_call
    def getinfo(self):
        self.procinfo = get_procinfo(self.pid)
    
    def update_view(self):
        self.getinfo()

        self.textBrowser_process.setText(self.procinfo)

        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Process("serverapp.exe")
    ui.setFixedSize(800, 600)
    ui.show()
    sys.exit(app.exec_())